<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/webjars/layui/2.5.5/css/layui.css">
</head>
<body class="login-bg">
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-block">
            <input type="text" name="userName" required  lay-verify="required" placeholder="请输入账号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-inline">
            <input type="password" name="name" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-inline">
            <input type="password" name="old" required lay-verify="required" placeholder="请输入年龄" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">单选框</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="男" title="男">
            <input type="radio" name="sex" value="女" title="女" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div id="opt" class="layui-input-block">
            <select name="city" lay-verify="required">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限</label>
        <div id="opt2" class="layui-input-block">
            <select name="city2" lay-verify="required">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门</label>
        <div id="opt3" class="layui-input-block">
            <select name="city3" lay-verify="required">

            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script type="text/javascript" src="/webjars/jquery/3.1.1/jquery.js"></script>
<script type="text/javascript" src="/webjars/layui/2.5.5/layui.js"></script>
<script>
    $(function () {
        /*全查询角色*/
        $.ajax({
            type:"post",
            url:"/SelectRole",
            data:{
            },
            async:true,
            dataType:"json",
            success:function (data) {
                $.each(data.data,function (index,item) {
                    var op="<option class='roleName'>"+item.roleName+"</option>"
                    $('select[name="city"]').append(op)
                })
            }
        },JSON)

        /*全查询权限*/
        $.ajax({
            type:"post",
            url:"/SelectDepartment",
            data:{
            },
            async:true,
            dataType:"json",
            success:function (data) {
                $.each(data.data,function (index,item) {
                    var op="<option class='roleName'>"+item.departmentName+"</option>"
                    $('select[name="city2"]').append(op)
                })
            }
        },JSON)

        /*全查询部门*/
        $.ajax({
            type:"post",
            url:"/SelectPermission",
            data:{
            },
            async:true,
            dataType:"json",
            success:function (data) {
                console.log(data)
                $.each(data.data,function (index,item) {
                    var op="<option class='roleName'>"+item.permissionName+"</option>"
                    $('select[name="city3"]').append(op)
                })
            }
        },JSON)

    })
    //Demo
    layui.use('form', function(){
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function(data){
            /*layer.msg(JSON.stringify(data.field));*/
            console.log(data)
            $.ajax({
                type:"post",
                url:"/Insert",
                data: JSON.stringify(data.field),
                async:true,
                contentType : "application/json;charsetset=UTF-8",//必须
                dataType:"json",
                success:function (data) {
                    if (data==true){
                        var index=parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                        parent.layer.close(index);
                        layer.msg("添加成功")
                        window.parent.location.reload();
                    }
                }, error: function () {
                    layer.msg("添加失败")
                }
            },JSON)
            return false;
        });


    });
</script>
</html>